{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 数据类型\n",
    "1. 数字可以是整数或者浮点数\n",
    "2. 字符串是不可变的\n",
    "3. None没有意义，可用于代表某些初始状态\n",
    "4. 元组通过()圆括号进行创建，不可改变\n",
    "5. 列表通过[]方括号创建，可以插入或者删除其中的值\n",
    "6. 字典通过{key:value}形式创建，代表键值对，就是哈希表，键和值的类型没有要求\n",
    "7. 集合通过{1,2}形式创建，其不包含重复的元素\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b8e23e5737544ed3"
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:05:28.173654900Z",
     "start_time": "2025-01-07T03:05:28.161430800Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "dict_keys(['key'])"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'key': 'value'}\n",
    "d.keys()"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "d['key'] = 10"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:05:46.602619300Z",
     "start_time": "2025-01-07T03:05:46.596484200Z"
    }
   },
   "id": "c11ca3d0609a62d2",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{'key': 10}"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:05:51.587900Z",
     "start_time": "2025-01-07T03:05:51.567885Z"
    }
   },
   "id": "4c5592bd0bd2a322",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "d.clear()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:05:57.272082Z",
     "start_time": "2025-01-07T03:05:57.263230600Z"
    }
   },
   "id": "1f3b2e7308428d21",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "{}"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:05:59.778764200Z",
     "start_time": "2025-01-07T03:05:59.772035400Z"
    }
   },
   "id": "5f658059c0e8e5af",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'k0VhGwKbfa'"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "import string\n",
    "\n",
    "def random_str(n):\n",
    "    s = string.ascii_letters + string.ascii_uppercase + string.digits\n",
    "    return ''.join(random.sample(s,n))\n",
    "\n",
    "random_str(10)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:07:55.245072Z",
     "start_time": "2025-01-07T03:07:55.233661300Z"
    }
   },
   "id": "3b9af63f1c13d807",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "[('k2', 2), ('k3', 3), ('k1', 5)]"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = {'k1': 5, 'k2': 2, 'k3': 3}\n",
    "\n",
    "d.items()\n",
    "sorted(d.items(), key=lambda x: x[1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:08:56.037724700Z",
     "start_time": "2025-01-07T03:08:56.004670600Z"
    }
   },
   "id": "3316c0a1977f4d02",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class Course:\n",
    "    def __init__(self,name,desc):\n",
    "        self.name = name\n",
    "        self.description = desc\n",
    "        self.labs = []\n",
    "    \n",
    "    def register_lab(self,lab):\n",
    "        self.labs.append(lab)\n",
    "    \n",
    "    @property\n",
    "    def lab_count(self):\n",
    "        return len(self.labs)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:12.295372800Z",
     "start_time": "2025-01-07T03:12:12.287421600Z"
    }
   },
   "id": "2be4b7ed65881b80",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "course = Course(name='Linux basic', desc='basic command, shell script ....')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:14.392446200Z",
     "start_time": "2025-01-07T03:12:14.383077500Z"
    }
   },
   "id": "6a849919a9f83fa0",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'Linux basic'"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "course.name"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:18.668842500Z",
     "start_time": "2025-01-07T03:12:18.658318Z"
    }
   },
   "id": "57973e4ad52d7fbc",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'basic command, shell script ....'"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "course.description"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:23.354380700Z",
     "start_time": "2025-01-07T03:12:23.344015300Z"
    }
   },
   "id": "370c405609e99550",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "0"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "course.lab_count"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:27.699586Z",
     "start_time": "2025-01-07T03:12:27.666149900Z"
    }
   },
   "id": "e2948006583682ce",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "course.register_lab('first lab')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:32.573035200Z",
     "start_time": "2025-01-07T03:12:32.561918400Z"
    }
   },
   "id": "5091c8ad6ba49351",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "course.lab_count"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:12:36.471370200Z",
     "start_time": "2025-01-07T03:12:36.460342600Z"
    }
   },
   "id": "ae903758cdba3192",
   "execution_count": 14
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 文件访问\n",
    "在 Python 中访问文件非常简单，主要通过 open 函数完成相关操作\n",
    "\n",
    "下面通过代码进行演示\n",
    "首先通过终端输入以下命令创建 hello.txt 文件\n",
    "Jupyter Notebook 中执行终端命令需要添加 !\n",
    "本地终端中无需添加"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2f003533132f23c3"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "!echo 'hello shiyanlou!' > hello.txt"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:13:47.486023700Z",
     "start_time": "2025-01-07T03:13:47.445943200Z"
    }
   },
   "id": "7598b1911ef19496",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "\"'hello shiyanlou!' \\n\""
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = open(\"hello.txt\")\n",
    "f.read()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:14:13.919319600Z",
     "start_time": "2025-01-07T03:14:13.907169300Z"
    }
   },
   "id": "d78c708d27d4c23",
   "execution_count": 16
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "f.close()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:14:21.376915600Z",
     "start_time": "2025-01-07T03:14:21.365580100Z"
    }
   },
   "id": "d491b46687310a54",
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{\"id\": 5348, \"name\": \"Luo\"}, {\"id\": 13, \"name\": \"Lei\"}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "d = [{'id':5348,'name':'Luo'},{'id':13,'name':'Lei'}]\n",
    "content = json.dumps(d)\n",
    "with open('users.json','w+') as f:\n",
    "    f.write(content)\n",
    "\n",
    "f = open('users.json')\n",
    "print(f.read())\n",
    "f.close()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-01-07T03:17:15.247436300Z",
     "start_time": "2025-01-07T03:17:15.230675900Z"
    }
   },
   "id": "1ea130760690ba29",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "e1c70e722df265c4"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
